package com.woniuxy.cloud.batteryassembly.controller;


import com.woniuxy.cloud.api.BatteryQueryApi;
import com.woniuxy.cloud.batteryassembly.dto.PageBatteryDto;
import com.woniuxy.cloud.batteryassembly.exception.BatteryInfoException;
import com.woniuxy.cloud.batteryassembly.service.IBatteryService;
import com.woniuxy.cloud.demo.Result;
import com.woniuxy.cloud.dto.BatteryDto;
import com.woniuxy.cloud.param.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gaorui
 * @since 2022-02-09
 */
@RestController
public class BatteryController implements BatteryQueryApi {

    @Resource
    private IBatteryService batteryService;

    @Override//根据ID查询电池信息
    public Result getById(Integer id) {


        return new Result().newInstance().setMsg("电池能正常使用").setCode(200).setData(batteryService.findById(id));
    }

    @Override//根据网点查询电池信息
    public Result queryBattery(ListBatteryParam listBatteryParam) {
        PageBatteryDto pageBatteryDto = batteryService.listBattery(listBatteryParam);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(pageBatteryDto);
    }

    @Override
    public List<BatteryDto> batteriesIsHealth(BatteriesIsHealthParam param) {
        return batteryService.batteriesIsHealth(param);
    }

    @Override//根据数组id查询电池信息
    public Result<List<BatteryDto>> findBatteryByIds(List<Integer> ids) {

        return new Result().newInstance().setMsg("查询成功").setCode(200).setData(batteryService.findBatteryByIds(ids));
    }

    @Override
    public Result addBattery(BatteryParam batteryParam) {
        System.out.println(batteryParam);

        boolean b = batteryService.addBattery(batteryParam);
        if (b) return new Result().newInstance().setMsg("添加成功").setCode(200);

        throw new BatteryInfoException("添加失败", 1002);
    }

    @Override//根据ID删除电池信息
    public Result delBattery(List<Integer> batteryIds) {

        if (batteryService.deleteBattery(batteryIds))
            return Result.newInstance().setCode(200).setMsg("删除成功");
        return Result.newInstance().setCode(500).setMsg("删除失败");
    }

    @Override//修改电池信息
    public Result updateBattery(BatteryParam batteryParam) {

        return new Result().newInstance().setMsg("修改成功,电池能正常使用").setCode(200).setData(batteryService.updateBattery(batteryParam));
    }

    @Override
    public Boolean updateBatteryById(UpdateParam updateParam) {

        return batteryService.updateBattery(updateParam);
    }

    @Override
    public Result<Boolean> updateList(UpdateListParam updateListParam) {

        return Result.newInstance().setCode(200).setMsg("修改成功").setData(batteryService.updateList(updateListParam));

    }

    @Override
    public Result addBatteryList(AddListBatteryParam addListBatteryParam) {
        if (batteryService.addBatteryList(addListBatteryParam)) {
            return Result.newInstance().setCode(200).setMsg("添加成功");
        }
        throw new BatteryInfoException("批量电池添加失败", 10789);

    }

    @PostMapping("/listBatteryByLocationIdAndTypeId/{locationId}/{typeId}")
    public Result<List<BatteryDto>> listBatteryByLocationIdAndTypeId(@PathVariable Integer locationId,@PathVariable Integer typeId) {
        return new Result().newInstance().setMsg("查询成功").setCode(200).setData(batteryService.listBatteryByLocationIdAndTypeId(locationId,typeId));
    }

}

